<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />

    <a-card
      :bordered="false"
      style="over-flow:scroll"
    >
      <div>
        <a-row>
          <a-col
            :md="24"
            :sm="19"
          >
            <div class="table-operator">
              <a-row>
                <a-col
                  :md="24"
                  :sm="24"
                >
                  <a-button
                    v-btnPermission="'pm_020_add_01'"
                    type="primary"
                    @click="goAdd('add')"
                  >
                    新增考核
                  </a-button>
                </a-col>
              </a-row>
            </div>

            <div class="pub-table-select-count">
              <a-icon
                type="info-circle"
                theme="twoTone"
              />
              <span style="margin-right: 12px">
                分包商考核信息：<span class="totalNum">{{ total.total }} 个 </span> |
                审核中：<span class="totalNum">{{ total.submitted }} 个 </span> |
                审核通过：<span class="totalNum">{{ total.past }} 个 </span> |
                审核不通过：<span class="totalNum">{{ total.rejected }} 个 </span>
              </span>
            </div>
            <list-table
              ref="table"
              size="default"
              row-key="row.id"
              :columns="columns"
              :request="loadData"
              :alert="true"
              show-size-changer
              show-quick-jumper
              show-pagination="auto"
            >
              <span
                slot="serial"
                slot-scope="text, record, index"
              >{{ index + 1 }}</span>
              <span
                slot="action"
                slot-scope="text, record"
              >
                <MoreButton>
                  <a
                    v-btnPermission="'pm_020_loo_02'"
                    @click="handleSub(record,'view')"
                  >查看</a>

                  <a v-if="record.statusCode=='0' || record.statusCode=='3'"
                    v-btnPermission="'pm_020_edi_03'"
                    @click="handleEdit(record,'edit')"
                  >编辑</a>
                  <a
                    v-if="record.statusCode=='1' || record.statusCode=='2'|| record.statusCode=='3'"
                    v-btnPermission="'pm_020_moi_04'"
                    @click="handleApproveDetail(record)"
                  >审批信息</a>
                </MoreButton>
              </span>
            </list-table>
          </a-col>
        </a-row>
      </div>
    </a-card>

    <!-- 审批详情 -->
    <ApprovalDetails
      :title="'审批详情'"
      :business-id="businessId"
      :visible="visibleApprove"
      @cancel="visibleApprove = false"
      @confirm="visibleApprove = false"
    />

    <a-modal
      v-if="visible"
      destroy-on-close
      :title="title"
      :visible="true"
      width="1000px"
      @cancel="visible=false"
    >
      <template slot="footer">
        <a-button
          key="back"
          @click="handleCancel"
        >
          取消
        </a-button>
        <a-button
          key="submit"
          :loading="loading"
          type="primary"
          @click="handleOk"
        >
          确认
        </a-button>
      </template>
      <title-name title="考核信息" />
      <a-form-model
        ref="ruleForm"
        :label-col="{span: 6 }"
        :wrapper-col="{span: 18 }"
        :rules="rules"
        :model="form"
      >
        <a-row>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="所属单位"
              :label-col="{lg: {span: 6}, sm: {span:6}}"
              :wrapper-col="{lg: {span: 18}, sm: {span: 18} }"
              prop="projectId"
            >
<!--              <SelectProject-->
<!--                v-model="form.projectId"-->
<!--                :auto-selected="false"-->
<!--                placeholder="请选择"-->
<!--                :is-clear="false"-->
<!--                :disabled="form.types=='view'"-->
<!--                :form="form"-->
<!--                key-value="branchCompany"-->
<!--                @change="getProjectId"-->
<!--              />-->
              <org-tree-select
              :disabled="form.types=='view'"
              v-model="form.projectId"
              is-need-auth
              check-strictly
              @select="selectOrg"></org-tree-select>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :md="12" :sm="12">-->
          <!--            <a-form-model-item label="分公司"-->
          <!--                               :labelCol="{lg: {span: 6}, sm: {span:6}}"-->
          <!--                               :wrapperCol="{lg: {span: 18}, sm: {span: 18} }" prop="branchCompany" >-->
          <!--              <SelectSubCompany :autoSelected='false' placeholder="请选择" v-model="form.branchCompany" @change="changeCompany" :disabled="form.types=='view'"></SelectSubCompany>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <!--          <a-col :md="12" :sm="12">-->
          <!--            <a-form-model-item label="项目名称"-->
          <!--                               :labelCol="{lg: {span: 6}, sm: {span:6}}"-->
          <!--                               :wrapperCol="{lg: {span: 18}, sm: {span: 18} }" prop="projectId">-->
          <!--              <SelectProject :autoSelected='false'  placeholder="请选择" v-model="form.projectId" :isClear="false" :disabled="form.types=='view'"-->
          <!--                             :form="form" keyValue="branchCompany" @change="getProjectId"></SelectProject>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="分包商"
              prop="subContractorId"
            >
              <a-select
                v-model="form.subContractorId"
                placeholder="请选择"
                show-search
                :filter-option="filterOption"
                allow-clear
                :disabled="form.types=='view'"
              >
                <a-icon
                  slot="suffixIcon"
                  type="search"
                />
                <a-spin
                  v-if="loading"
                  slot="notFoundContent"
                  size="small"
                />
                <a-select-option
                  v-for="item in subList"
                  :key="item.subcontractorsId"
                  :value="item.subcontractorsId"
                >
                  {{ item.subcontractors }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="考核评价"
              prop="evaluate"
            >
              <a-select
                v-model="form.evaluate"
                placeholder="请选择"
                :disabled="form.types=='view'"
              >
                <a-spin
                  v-if="loading"
                  slot="notFoundContent"
                  size="small"
                />
                <a-select-option
                  v-for="item in statusCodes"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="审核类型"
              prop="type"
            >
              <a-select
                v-model="form.type"
                placeholder="请选择"
                :disabled="form.types=='view'"
              >
                <a-spin
                  v-if="loading"
                  slot="notFoundContent"
                  size="small"
                />
                <a-select-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="情况说明"
              prop="information"
            >
              <BaseTextarea
                v-model="form.information"
                :disabled="form.types=='view'"
                :max-length="300"
                type="textarea"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <title-name title="相关证明附件" />
      <div>
        <a-row>
          <a-col
            :md="24"
            :sm="19"
          >
            <div class="table-operator">
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    label="附件上传"
                    :label-col="{ lg: 2, md: 3, sm: 3 }"
                    :wrapper-col="{ lg: 22, md: 21, sm: 21 }"
                  >
                    <div>
                      <upload-list
                        :business-code="'PM11'"
                        :edit-model="2"
                        :show-upload.sync="isClose"
                        :disabled="form.types=='view'"
                        :upload-arrys.sync="form.proveCommonIdList"
                        @fileArrysData="fileArrysData"
                      />
                    </div>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </div>
          </a-col>
        </a-row>
      </div>
    </a-modal>


  <!--<add :form = "form" :title="title" :isClose="isClose" :loading="loading" :visible="visible" @handleok="handleok" @handleCancel="handleCancel"></add>-->
    <!-- 选择审批人 -->
      <selectApprover
        :business-id="businessId1"
        :process-config="setProcessConfig"
        :is-close-upper="false"
        @close="closeSelectApprover"
      />
      <!-- 选择审批人 -->
  </base-breadcrumb>
</template>

<script>
  import Vue from 'vue'
  import { STable, Ellipsis } from '@/components'
  import {addEvaluateList,statisticSassessment} from '@/api/evaluate'
  import { getassessmentList ,addassessmentList ,editassessmentList,statistics,getDetails} from '@/api/subcontractors/managerAssessment'
  import { getSubcontractorsList } from '@/api/subcontractors/standing'
  import { getSubcontractorsByProject } from '@/api/subcontractors/standing'
  import {getStatusCodeData } from '@/api/subcontractors/labor'
  import { themeChoose } from '@/mixin/mixin'
  import { getCompany } from '@/api/themeChoose/themeChoose'
  import UploadList from '@/components/UploadList'
  import Add from './add.vue'
  import ApprovalDetails from '@/components/approvalDetails'
  import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
  import SelectProject from '@/components/Select/SelectProject' // 项目
  import OrgTreeSelect from '@/components/OrgTreeSelect'
  const columns = [
    {
      title: '序号',
      width:65,
      scopedSlots: { customRender: 'serial' }
    },
    {
      title: '考核分包商名称',
      width:120,
      dataIndex: 'name'
    },
    {
      title: '项目',
      width:100,
      dataIndex: 'project',
    },
    {
      title: '分公司',
      width:100,
      dataIndex: 'branchCompany',
    },
    {
      title: '项目经理',
      width:100,
      dataIndex: 'manager',
    },
    {
      title: '考核评价',
      width:100,
      dataIndex: 'evaluate',
    },
    {
      title: '审核类型',
      width:100,
      dataIndex: 'type',
    },
    {
      title: '创建时间',
      width:100,
      dataIndex: 'createTime',
    },
    {
      title: '状态 ',
      width:100,
      dataIndex: 'status',
    },
    {
      title: '操作',
      dataIndex: 'action',
      width: 200,
      scopedSlots: { customRender: 'action' }
    }
  ]
  export default {
    name:'a'+Date.now(),
   mixins:[themeChoose],
    components:{
      SelectSubCompany,
      SelectProject,
      STable,
      UploadList,
      ApprovalDetails
    },
    data(){
      this.columns = columns
      return{
        projectId:'',
        pageNo: 1,
        pageSize: 20,


        fileList:[],
        statusCodes:[],
        subList:[],
        disabled:false,
        typeList:[],
        type:'',
        title:'',
        businessId:'',
        visibleApprove:false,
        isClose:true,
        confirmLoading:false,
        loading:false,
        form:{},
        visible:false,        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.statistics()
          const requestParameters = Object.assign({}, parameter, this.queryParam)
          return getassessmentList(requestParameters)
            .then(res => {
              return res
            })
        },
        selectedRowKeys:'',
        selectedRows:'',
        total:{},
      // 校验规则
      rules: {
        branchCompany: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
        subContractorId: [{ required: true, message: '请选择分包商', trigger: 'change' }],
        evaluate: [{ required: true, message: '请选择考核评价', trigger: 'change' }],
        type: [{ required: true, message: '请选择审核类型', trigger: 'change' }],
        information: [{ required: true, message: '请输入情况说明', trigger: 'change' }],
      },
        searchArr: [
          // 搜索框
          {
            name: '分包商名称',
            value: 'name',
            type: 'input'
          },
          {
            name: '考核评价',
            value: 'evaluate',
            type: 'select',
            data:[]
          },
          // {
          //   name: '项目名称',
          //   value: 'project',
          //   type: 'input'
          // },
         {
            name: '所属单位',
            valueKey: 'searchQuery',
            type: OrgTreeSelect,
          },
          {
            name: '状态',
            type: 'select',
            value: 'status',
            data: [
              {
                value: '1',
                label: '审核中'
              },
              {
                value: '2',
                label: '审核通过'
              },
              {
                value: '3',
                label: '审核不通过'
              },
            ]
          },
        ],
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId1: null
      }
    },
   async created() {
      this.statistics()
      this.getStatusCodeData()
      this.processConfig = await this.$getConfigFn('project_manager_assesses_subcontractors')
    },
    methods:{
      selectOrg(val) {
        if(val&&val.length) {
          this.form.branchCompany = val[val.length - 2].orgId
          this.$set(this.form, "subContractorId" , null)
          this.getSubcontractorsList(this.form.projectId)
        }
      },
      getProjectId(val){
        // this.getSubcontractorsByProject(val)
      },
      scrollMore(e) {
        const { target } = e
        var total = target.scrollTop + target.offsetHeight
        var scrollHeight = target.scrollHeight

        // this.pageNo是当前页 this.totalPage是总页数
        if (total >= scrollHeight - 1 && total < scrollHeight + 1 && this.pageNo <= this.totalPage ) {
          this.pageNo = this.pageNo + 1
          this.getSubcontractorsByProject() // 调用api方法
        }
      },
      getSubcontractorsByProject(val){
        this.loading=true
        getSubcontractorsByProject({projectId:val,pageSize: this.pageSize, pageNo: this.pageNo}).then(res=>{
          this.subList = res.data
          this.loading=false
        })
      },
      changeCompany(value){
        console.log(value)
        this.$set(this.form,'projectId','')
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      // getStatusCodeData(){
      //   this.loading=true
      //   getStatusCodeData({type:4, enabledCode:1}).then(res=>{
      //     this.statusCodes = res.data
      //     this.loading=false
      //     let arr = []
      //     res.data.forEach(function(obj,i) {
      //       let item = {}
      //       if(obj){
      //         item.value = obj.id
      //         item.label = obj.name
      //       }
      //       arr.push(item)
      //     })
      //     console.log(arr)
      //   })
      //   getStatusCodeData({type:6}).then(res=>{
      //     this.typeList = res.data
      //     this.loading=false
      //   })
      // },

      handleApproveDetail(record){
        this.visibleApprove = true
        this.businessId = record.id
      },
      getStatusCodeData(){
        getStatusCodeData({type:4, enabledCode:1}).then(res=>{
            this.statusCodes = res.data
              let arr = []
              res.data.forEach(function(obj,i) {
                let item = {}
                if(obj){
                  item.value = obj.id
                  item.label = obj.name
                }
                arr.push(item)
              })
              console.log(arr)
            this.searchArr[1].data = arr
        })
        getStatusCodeData({type:6}).then(res=>{
            this.typeList = res.data
        })
      },
      // 列表统计
      statistics(){
        statistics(this.queryParam).then(res=>{
          this.total = res.data
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        console.log(selectedRowKeys)
        console.log(selectedRows)
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      search(values){
        this.queryParam = values
        this.$refs.table.refresh(true)
        this.statistics()
      },
      goAdd(type){
        this.title="新增分包商考核"
        this.form = {}
        this.form.proveCommonIdList = []
        this.visible = true
        this.type=type
        // this.getSubcontractorsList()
      },
      handleSub(record,type){
        this.$router.push({
          path:"/project/assessment/detail",
          query:{
            id:record.id,
            type:type
          }
        })

      },
      handleEdit(record,type){
        this.title="编辑分包商考核"
        this.type=type
        this.visible = true
        this.$set(this.form, "subContractorId" , null)
        this.getSubcontractorsList(record.projectId,)
        // this.form = record
        getDetails({id:record.id}).then(res=>{

          this.form = res.data
          this.form.branchCompany = this.form.branchCompanyId
          this.form.evaluate = res.data.evaluateId
          this.form.type = res.data.typeId
          this.form.types = type
          this.form.proveCommonIdList = res.data.proveFileList


          this.$nextTick(()=>{
            this.form.subContractorId = res.data.subContractorId
          })

          this.isClose = false
        })
      },
      fileArrysData(val){
        this.form.proveCommonIdList = val
      },
      handleOk(){
        this.setProcessConfig = null
        this.$refs.ruleForm.validate(valid=>{
          if(valid){
            this.submitApprovalConfirm(this.processConfig, (flag)=>{
              if(!flag) {
                this.loading = false
                return
              }
              if(this.type=='view'){
                this.visible=false
              }else {
                if(this.form.id){
                  this.loading = true
                  editassessmentList(this.form).then(res=>{
                    this.loading = false
                    this.visible = false
                    if(res.code==200){
                      this.$message.success("编辑成功")
                      this.setProcessConfig = this.processConfig
                      this.businessId1 = res.data.id || this.form.id
                    }
                    this.$refs.table.refresh(true)
                  })
                }else{
                  this.loading = true
                  addassessmentList(this.form).then((res)=>{
                    this.loading = false
                    if(res.code==200){
                      this.visible = false
                      this.$message.success("新增成功")
                      this.setProcessConfig = this.processConfig
                      this.businessId1 = res.data.id + '' || this.form.id
                    }
                    this.$refs.table.refresh(true)
                  })
                }
            }
            })
          } else {
            this.$message.warn("请填写完所有必填项信息")
          }
        })
      },
      getSubcontractorsList(val){
        this.loading = true
        getSubcontractorsByProject({projectId:val,isPage:1}).then(res=>{
          this.subList = res.data
          this.loading = false
        })
      },
      handleCancel(){
        this.visible = false
      }
    }
  }
</script>

<style scoped>
  .input{
    width:100%
  }
  /*/deep/ .ant-table-pagination.ant-pagination{*/
  /*  display:none*/
  /*}*/
</style>
